package com.hualan.dao.goodsdao.impl;

import com.hualan.bean.PanghuShop;
import com.hualan.bean.PanghuShopGoods;
import com.hualan.dao.shopdao.ShopDao;
import com.hualan.dao.shopdao.impl.ShopDaoImpl;
import com.hualan.utils.C3p0Util;

import java.sql.Array;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl {
	
	
	private ShopDao dao;
	
	public int addGoods(PanghuShopGoods goods) throws SQLException {
		String addSql = "insert into panghu_shop_goods(" +
				                "goods_name," +
				                "goods_image," +
				                "goods_present_price," +
				                "shop_id," +
				                "add_time," +
				                "goods_status) values (?,?,?,?,?,?)";
		Connection connection = C3p0Util.getConnection();
		System.out.println(goods);
		int update = C3p0Util.update(addSql, connection, goods.getGoodsName(), goods.getGoodsImage(), goods.getGoodsPresentPrice(), goods.getShopId(), goods.getAddTime(), goods.getGoodsStatus());
		connection.commit();
		return update;
	}
	
	public Map<String,Object> selectGoods(Map<String, String> map) {
		dao = new ShopDaoImpl();
		StringBuilder s = new StringBuilder("select goods_id,goods_name,goods_image,goods_present_price,goods_status,shop_id,add_time from panghu_shop_goods where 1 = 1");
		Connection connection = C3p0Util.getConnection();
		StringBuilder ids = new StringBuilder();
		List<Object> list = new ArrayList<>();
		Long total;
		Integer pageNum = Integer.parseInt(map.get("pageNum"));
		Integer pageSize = Integer.parseInt(map.get("pageSize"));
		
		if (map.size() == 2) {
			total = getCountCondition(s.toString());
			Map<String, Object> encapsulation = encapsulation(pageNum, pageSize, total);
			s.append(" limit ?,?");
			List<PanghuShopGoods> data = C3p0Util.query(s.toString(), PanghuShopGoods.class, (pageNum - 1) * pageSize,pageSize);
			encapsulation.put("list",data);
			return encapsulation;
		}
		
		if (map.containsKey("goodsStatus")) {
			s.append(" and goods_status = ?");
			list.add(Integer.parseInt(map.get("goodsStatus")));
		}
		
		if (map.containsKey("goodsName")) {
			s.append(" and goods_name like ?");
			list.add("%" + map.get("goodsName") + "%");
		}
		
		
		if (map.containsKey("shopName")) {
			s.append(" and shop_id in (");
			List<PanghuShop> shopId = dao.getIdByName(map.get("shop_name"));
			for (int i = 0; i < shopId.size(); i++) {
				if (i == shopId.size() - 1) {
					s.append(shopId.get(i).getShopId());
				} else {
					s.append(shopId.get(i).getShopId()).append(",");
				}
			}
			s.append(")");
//			System.out.println(ids.toString());
//			list.add(ids.toString());
		}
		
		List<PanghuShopGoods> dataCondition = C3p0Util.query(s.toString(), PanghuShopGoods.class, list.toArray());

		total = (long) dataCondition.size();
		System.out.println(total);
		s.append(" limit ?,?");
		list.add((pageNum -1) * pageSize);
		list.add(pageSize);
		List<PanghuShopGoods> data = C3p0Util.query(s.toString(), PanghuShopGoods.class, list.toArray());
		Map<String, Object> encapsulation = encapsulation(pageNum, pageSize, total);
		encapsulation.put("list",data);
		return encapsulation;
	}
	
	public Map<String,Object> selectShopGoods(Map<String, String> map) {
		List<String> ps = new ArrayList<String>();
		HashMap<String, Object> map1 = new HashMap<>();
		String sql = "select goods_id,goods_name,goods_image,goods_present_price,shop_id from panghu_shop_goods where shop_id = ? ";
		StringBuilder builder = new StringBuilder(sql);
		ps.add(map.get("shopId"));
		if (map.containsKey("keyword")){
			builder.append(" and goods_name like ?");
			ps.add("%"+map.get("keyword")+"%");
		}
		if (map.containsKey("goodsStatus")){
			builder.append("and goods_status=?");
			ps.add(map.get("goodsStatus"));
		}
		List<PanghuShopGoods> query = C3p0Util.query(builder.toString(), PanghuShopGoods.class, ps.toArray());
//		System.out.println(query);
		map1.put("list",query);
		return map1;
	}
	
	
	
	public Long getCountCondition(String sql) {
		String countSql = "select count(*) count from " + "("+sql+")" +" countSelect";
		Map<String, Object> stringObjectMap = C3p0Util.queryOne(countSql);
		return (Long) stringObjectMap.get("count");
	}
	
	
	
	
	private Map<String,Object> encapsulation(Integer pageNum,Integer pageSize,Long total){
		Map<String,Object> data = new HashMap<>();
		data.put("total",total);
		data.put("isFirstPage",pageNum == 1);
		// 计算当前是第几行开始
		
		int startRow = (pageNum - 1) * pageSize + 1;
		data.put("startRow", startRow);
		
		int endRow;
		// 第几行结束
		if (pageNum != (int) Math.ceil((double) ( total / pageSize))){
			endRow = startRow + pageSize - 1;
			data.put("endRow", endRow);
		} else {
			int endPageRows = (int) (total % pageSize);
			endRow = startRow + (endPageRows - 1);
			data.put("endRow",endRow);
		}
		
		/**
		 * 计算总共有多少页
		 */
		data.put("isLastPage",pageNum == Math.ceil((double) (total / pageSize)));
		data.put("firstPage",1);
		data.put("lastPage",(int) Math.ceil((double) (total / pageSize)));
		
		// 页码
		ArrayList<Integer> integers = new ArrayList<>();
		int p = (int) Math.abs(Math.ceil((double) (total / pageSize)));
		for (int i = 1; i <= p ; i++) {
			integers.add(i);
		}
		data.put("navigatepageNums",integers);
		data.put("nextPage",pageNum == pageSize ? -1 : pageNum + 1);
		data.put("prePage",pageNum == 1 ? -1 : pageNum - 1);
		return data;
	}
	
	public int updateStatus(String goodsStatus, String goodsId) {
		Connection connection = null;
		try {
			String updateSql = "update panghu_shop_goods set goods_status = ? where goods_id = ?";
			connection = C3p0Util.getConnection();
			int update = C3p0Util.update(updateSql, connection, goodsStatus, goodsId);
			connection.commit();
			return update;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException ex) {
				throw new RuntimeException(ex);
			}
			throw new RuntimeException(e);
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		
	}
	
	
}
